package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import com.it2297.controller.EmailController;
import com.it2297.util.Email;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.MatteBorder;

public class CompanySendEmail extends JFrame {

	private JPanel contentPane;
	private JTextField tf_to;
	private JTextField tf_from;
	private JTextField tf_subject;
	private JTextArea tf_msg;
	private String subject;
	private String message;
	final EmailController ec= new EmailController(this);
	
	public String getFromEmail(){
		return tf_from.getText();
	}
	
	
	public String getToEmail(){
		return tf_to.getText();
	}
	
	public String getSubject(){
		return tf_subject.getText();
		
	}
	
	public String getMessage(){
		return tf_msg.getText();
	}
	
	public void setToEmail(String to){
		tf_to.setText(to);
	}
	
	public void setSubject(String subject){
		tf_subject.setText(subject);
		//this.subject=subject;
	}
	
	public void setMessage(String msg){
		tf_msg.setText(msg);
		//this.message=msg;
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CompanySendEmail frame = new CompanySendEmail();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CompanySendEmail() {
		String currentWorkingDirectory = System.getProperty("user.dir");
		setTitle("Send Email");
	//	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		setLocationRelativeTo(null);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		tf_to = new JTextField();
		tf_to.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		tf_to.setText("IT2297Recipient@hotmail.com");
		tf_to.setBounds(72, 97, 304, 28);
		contentPane.add(tf_to);
		tf_to.setColumns(10);
		
		tf_from = new JTextField();
		tf_from.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
	//	tf_from.setText("IT2297@hotmail.com");
		tf_from.setText("IT2297OOADPROJECT@gmail.com");
		tf_from.setBounds(72, 141, 304, 28);
		contentPane.add(tf_from);
		tf_from.setColumns(10);
		
		tf_subject = new JTextField();
		//tf_subject.setText(subject);
		tf_subject.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		tf_subject.setBounds(72, 192, 378, 28);
		contentPane.add(tf_subject);
		tf_subject.setColumns(10);
		
		JLabel lblTo = new JLabel("To:");
		lblTo.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblTo.setBounds(72, 75, 62, 28);
		contentPane.add(lblTo);
		
		JLabel lblFrom = new JLabel("From:");
		lblFrom.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFrom.setBounds(72, 119, 62, 28);
		contentPane.add(lblFrom);
		
		JLabel lblNewLabel = new JLabel("Subject:");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel.setBounds(72, 169, 62, 28);
		contentPane.add(lblNewLabel);
		
		JLabel lblMessage = new JLabel("Message:");
		lblMessage.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMessage.setBounds(72, 219, 62, 28);
		contentPane.add(lblMessage);
		
		JButton btnSend = new JButton();
		btnSend.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/sendIcon.png"));
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(tf_to.getText().isEmpty()|| tf_from.getText().isEmpty()||tf_msg.getText().isEmpty()||tf_subject.getText().isEmpty()){
					JOptionPane.showMessageDialog(null, "Please fill in all the required fields.");
				}
				else{
					ec.sendEmail();
			//		tf_msg.setText("");
			//		tf_subject.setText("");
					closeWindow();
				}	
			}
		});
		btnSend.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnSend.setBounds(72, 393, 41, 39);
		contentPane.add(btnSend);
		
		JLabel lblSendEmail = new JLabel("Email System");
		lblSendEmail.setFont(new Font("Tahoma", Font.BOLD, 40));
		lblSendEmail.setBounds(23, 11, 691, 49);
		contentPane.add(lblSendEmail);
		
		tf_msg = new JTextArea();
		//tf_msg.setText(message);
		tf_msg.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		tf_msg.setBounds(72, 239, 587, 150);
		contentPane.add(tf_msg);
		
		/*JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				closeWindow();
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnBack.setBounds(198, 459, 120, 36);
		contentPane.add(btnBack);
		*/
		
		JLabel img = new JLabel("");
		img.setHorizontalAlignment(SwingConstants.CENTER);
		img.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/email.jpg"));
		img.setBounds(0, 0, 724, 522);
		contentPane.add(img);
		
		JLabel icon = new JLabel("");
		icon.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/mailIcon.png"));
		icon.setBounds(295, 11, 67, 54);
		contentPane.add(icon);
		
	
	}
	public void closeWindow(){
		this.dispose();
	}
}
